package org.springframework.beans.factory.support;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;

/**
 * @Name SimpleInstantiationStrategy
 * @Description: Bean 实例化策略 [实现] 类
 * 描述信息：简单的 Bean 实例化策略，根据 Bean 的无参构造函数实例化对象。
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-03-15 13:47:23
 **/
public class SimpleInstantiationStrategy implements InstantiationStrategy {

    @Override
    public Object instantiate(BeanDefinition beanDefinition) throws BeansException {
        Class beanClass = beanDefinition.getBeanClass();
        try {
            Constructor constructor = beanClass.getDeclaredConstructor();
            return constructor.newInstance();
        } catch (Exception e) {
            throw new BeansException("实例化 Bean [" + beanClass.getName() + "] 失败", e);
        }
    }

}

